package com.chenyun.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.UUID;
import com.chenyun.satoken.starter.util.SaUtil;
import com.chenyun.system.controller.SysMenuController;
import com.chenyun.system.mapper.SysMenuMapper;
import com.chenyun.system.mapper.SysRoleMapper;
import com.chenyun.system.mapper.SysRoleMenuMapper;
import com.chenyun.system.model.SysMenu;
import com.chenyun.system.model.SysRole;
import com.chenyun.system.service.ISysMenuService;
import com.chenyun.system.vo.MetaVo;
import com.chenyun.system.vo.RouterVo;
import com.chenyun.system.vo.TreeSelect;
import com.chenyun.common.constants.SysConstants;
import com.chenyun.common.constants.UserConstants;
import org.apache.commons.lang3.StringUtils;
import org.jacoco.agent.rt.internal_3570298.core.internal.flow.IFrame;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 菜单 业务层处理
 *
 * @author ruoyi
 */
@Service
public class SysMenuServiceImpl implements ISysMenuService {
    public static final String PREMISSION_STRING = "perms[\"{0}\"]";

    @Autowired
    private SysMenuMapper menuMapper;

    @Autowired
    private SysRoleMapper roleMapper;

    @Autowired
    private SysRoleMenuMapper roleMenuMapper;

    /**
     * 根据用户查询系统菜单列表
     *
     * @param userId 用户ID
     * @return 菜单列表
     */
    @Override
    public List<SysMenu> selectMenuList(Long userId) {
        return selectMenuList(new SysMenu(), userId);
    }

    /**
     * 查询系统菜单列表
     *
     * @param menu 菜单信息
     * @return 菜单列表
     */
    @Override
    public List<SysMenu> selectMenuList(SysMenu menu, Long userId) {
        List<SysMenu> menuList = null;
        // 管理员显示所有菜单信息
        if (SaUtil.isAdmin()) {
            menuList = menuMapper.selectMenuList(menu);
        } else {
            menu.getParams().put("userId", userId);
            menuList = menuMapper.selectMenuListByUserId(menu);
        }
        return menuList;
    }

    /**
     * 根据用户ID查询权限
     *
     * @param userId 用户ID
     * @return 权限列表
     */
    @Override
    public Set<String> selectMenuPermsByUserId(Long userId) {
        List<String> perms = menuMapper.selectMenuPermsByUserId(userId);
        Set<String> permsSet = new HashSet<>();
        for (String perm : perms) {
            if (StringUtils.isNotEmpty(perm)) {
                permsSet.addAll(Arrays.asList(perm.trim().split(",")));
            }
        }
        return permsSet;
    }

    /**
     * 根据用户ID查询菜单
     *
     * @param userId 用户名称
     * @return 菜单列表
     */
    @Override
    public List<SysMenu> selectMenuTreeByUserId(Long userId) {
        List<SysMenu> menus = null;
        if (SaUtil.isAdmin()) {
            menus = menuMapper.selectMenuTreeAll(2,null);
        } else {
            menus = menuMapper.selectMenuTreeByUserId(userId,2,null);
        }
        return getChildPerms(menus, 0);
    }

    /**
     * 根据用户ID查询所有按钮
     *
     * @param userId    为空查询管理员角色
     * @param parentIds 父级菜单id
     * @return
     */
    @Override
    public List<SysMenu> selectMenuAction(List<String> parentIds) {
        List<SysMenu> menus = null;
        String userId = SaUtil.getUserId();
        if(SaUtil.isAdmin()){
            //查询所有
            menus =menuMapper.selectMenuTreeAll(3,parentIds);
        }else {
            menus =menuMapper.selectMenuTreeByUserId(Long.valueOf(userId),3,parentIds);
        }
        return menus;
    }

    /**
     * 根据角色ID查询菜单树信息
     *
     * @param roleId 角色ID
     * @return 选中菜单列表
     */
    @Override
    public List<Long> selectMenuListByRoleId(Long roleId) {
        SysRole role = roleMapper.selectRoleById(roleId);
        return menuMapper.selectMenuListByRoleId(roleId, role.isMenuCheckStrictly());
    }

    /**
     * 根据角色ID查询菜单树信息
     *
     * @param roleId 角色ID
     * @return 返回菜单列表
     */
    @Override
    public List<SysMenu> queryMenuListByRoleId(String roleId) {
        return menuMapper.queryMenuListByRoleId(roleId);
    }

    /**
     * 构建前端路由所需要的菜单
     *
     * @param menus 菜单列表
     * @return 路由列表
     */
    @Override
    public List<RouterVo> buildMenus(List<SysMenu> menus) {
        List<RouterVo> routers = new LinkedList<RouterVo>();
        for (SysMenu menu : menus) {
            if (menu.getMenuId() == 116) {
                System.out.println("系统接口");
            }

            RouterVo router = new RouterVo();
            router.setHidden("1".equals(menu.getVisible()));
            router.setName(getRouteName(menu));
            router.setPath(getRouterPath(menu));
            router.setComponent(getComponent(menu));
            router.setMenuType(menu.getMenuType());
            router.setMenuSort(Integer.valueOf(menu.getOrderNum()));
            router.setQuery(menu.getQuery());
            MetaVo metaVo = new MetaVo(
                    menu.getMenuName(),
                    menu.getIcon(),
                    StringUtils.equals("1", menu.getIsCache()),
                    menu.getPath(),
                    menu.getIsFrame(),
                    menu.getTargetType(), menu.getQuery(),
                    menu.getPerms());
            metaVo.setShowLink(router.getHidden());
            metaVo.setRank(router.getMenuSort());
            router.setMeta(metaVo);
            router.setId(menu.getMenuId().toString());
            router.setPid(menu.getParentId().toString());
            List<SysMenu> cMenus = menu.getChildren();
            if (!cMenus.isEmpty() && cMenus.size() > 0 && UserConstants.TYPE_DIR.equals(menu.getMenuType())) {
                router.setAlwaysShow(true);
                router.setRedirect(null);
                router.setChildren(buildMenus(cMenus));
            } else if (isMenuFrame(menu)) {
                //不是外链
                router.setMeta(null);
                List<RouterVo> childrenList = new ArrayList<RouterVo>();
                RouterVo children = new RouterVo();
                children.setPath(menu.getPath());
                children.setComponent(menu.getComponent());
                children.setName(StringUtils.capitalize(menu.getPath()));

                children.setMeta(new MetaVo(menu.getMenuName(), menu.getIcon(),
                        StringUtils.equals("1", menu.getIsCache()), menu.getPath(), menu.getIsFrame(), menu.getTargetType(), menu.getQuery(), menu.getPerms()));
                children.setQuery(menu.getQuery());
                children.setId(menu.getMenuId().toString());
                children.setPid(menu.getParentId().toString());
                children.setMenuType(menu.getMenuType());
                children.setMenuSort(Integer.valueOf(menu.getOrderNum()));
                childrenList.add(children);
                router.setChildren(childrenList);
            } else if (menu.getParentId().intValue() == 0 && isInnerLink(menu)) {
                router.setMeta(new MetaVo(menu.getMenuName(), menu.getIcon()));
                router.setPath("/inner");
                List<RouterVo> childrenList = new ArrayList<RouterVo>();
                RouterVo children = new RouterVo();
                String routerPath = innerLinkReplaceEach(menu.getPath());
                children.setPath(routerPath);
                children.setComponent(UserConstants.INNER_LINK);
                children.setName(StringUtils.capitalize(routerPath));
                children.setMeta(new MetaVo(menu.getMenuName(), menu.getIcon(),
                        StringUtils.equals("1", menu.getIsCache()), menu.getPath(), menu.getIsFrame(), menu.getTargetType(), menu.getQuery(), menu.getPerms()));


                children.setId(menu.getMenuId().toString());
                children.setPid(menu.getParentId().toString());
                children.setMenuType(menu.getMenuType());
                children.setMenuSort(Integer.valueOf(menu.getOrderNum()));
                childrenList.add(children);
                router.setChildren(childrenList);
            }


            routers.add(router);
        }
        return routers;


    }

    /**
     * 构建pure-admin菜单
     * 规定只有三级菜单 全使用path模式
     * path模式子菜单必须有name属性,且和组件中name属性一致
     *
     * @param menus
     * @return
     */
    public List<Map<String, Object>> buildMenusPureAdmin(List<SysMenu> menus) {

        List<Map<String, Object>> routeList = new ArrayList<>();
        //拿到所有的菜单 筛选按钮
        List<SysMenu> newMenus = menus.stream().filter(p -> p.getMenuType().equals("C")).collect(Collectors.toList());
        List<SysMenu> actionMenus = this.selectMenuAction(newMenus
                .stream()
                .map(p -> String.valueOf(p.getMenuId())).collect(Collectors.toList()));
        for (SysMenu sysMenu : menus) {
            Map<String, Object> firstMenu = new HashMap<>();
            Map<String, Object> firstMetaMenu = new HashMap<>();
            List<SysMenu> children2 = sysMenu.getChildren();
            firstMenu.put("path", getPath(sysMenu.getPath()));
            firstMetaMenu.put("title", sysMenu.getMenuName());
            if (sysMenu.getMenuId().equals("600")){
                System.out.println("600");
            }
            //直接是菜单
            if (sysMenu.getMenuType().equals("C")) {
                //构建菜单
                firstMetaMenu.put("showLink", sysMenu.getVisible().equals("0"));
                firstMetaMenu.put("keepAlive", sysMenu.getIsCache().equals("0"));
                firstMenu.put("name", sysMenu.getPathName());
                this.getIframeUrl(sysMenu, firstMenu, firstMetaMenu);
                //查询按钮权限
                Object[] objects = actionMenus.stream().filter(p -> p.getParentId().intValue() == sysMenu.getMenuId().intValue())
                        .map(p -> {
                            return String.valueOf(p.getPerms());
                        })
                        .toArray();
                firstMetaMenu.put("auths",objects);
                firstMenu.put("meta", firstMetaMenu);
                routeList.add(firstMenu);
                continue;
            }

            if (CollectionUtil.isNotEmpty(children2)) {
                //二级菜单
                List<Map<String, Object>> routeList2 = new ArrayList<>();
                for (SysMenu menu : children2) {
                    Map<String, Object> twoMenu = new HashMap<>();
                    List<SysMenu> children3 = menu.getChildren();
                    Map<String, Object> twoMetaMenu = new HashMap<>();
                    twoMetaMenu.put("title", menu.getMenuName());
                    //判断是否还有子级
                    if (CollectionUtil.isNotEmpty(children3)) {
                        //还有三级菜单
                        twoMenu.put("path", menu.getPath());
                    } else {
                        twoMenu.put("path", getPath(menu.getPath()));
                        twoMetaMenu.put("showLink", menu.getVisible().equals("0"));
                        twoMetaMenu.put("keepAlive", menu.getIsCache().equals("0"));
                        //前端对应的组件名称
                        twoMenu.put("name", menu.getPathName());
                        if (children2.stream().filter(p->p.getVisible().equals("0")).count()== 1) {
                            twoMetaMenu.put("showParent", true);
                        }
                        //构建ifrem
                        this.getIframeUrl(menu, twoMenu, twoMetaMenu);
                        //查询按钮权限
                        Object[] objects = actionMenus.stream().filter(p -> p.getParentId().intValue() == sysMenu.getMenuId().intValue())
                                .map(p -> {
                                    return String.valueOf(p.getPerms());
                                })
                                .toArray();
                        twoMetaMenu.put("auths",objects);
                    }

                    twoMenu.put("meta", twoMetaMenu);


                    if (CollectionUtil.isNotEmpty(children3)) {
                        //三级菜单
                        List<Map<String, Object>> routeList3 = new ArrayList<>();

                        for (SysMenu sysMenu3 : children3) {
                            Map<String, Object> threeMenu = new HashMap<>();
                            Map<String, Object> threeMetaMenu = new HashMap<>();
                            threeMetaMenu.put("title", sysMenu3.getMenuName());
                            if (children3.stream().filter(p->p.getVisible().equals("0")).count()== 1 ) {
                                threeMetaMenu.put("showParent", true);
                            }

                            threeMetaMenu.put("showLink", sysMenu3.getVisible().equals("0"));
                            threeMetaMenu.put("keepAlive", sysMenu3.getIsCache().equals("0"));
                            threeMenu.put("path", getPath(sysMenu3.getPath()));
                            threeMenu.put("name", sysMenu3.getPathName());
                            //构建ifrem
                            this.getIframeUrl(sysMenu3, threeMenu, threeMetaMenu);
                            //查询按钮权限
                            Object[] objects = actionMenus.stream().filter(p -> p.getParentId().intValue() == sysMenu.getMenuId().intValue())
                                    .map(p -> {
                                        return String.valueOf(p.getPerms());
                                    })
                                    .toArray();
                            threeMetaMenu.put("auths",objects);
                            threeMenu.put("meta",threeMetaMenu);
                            routeList3.add(threeMenu);
                        }
                        twoMenu.put("children", routeList3);
                    }

                    routeList2.add(twoMenu);
                }
                firstMenu.put("children", routeList2);

            }
            firstMenu.put("meta", firstMetaMenu);
            routeList.add(firstMenu);

        }
        return routeList;
    }

    /**
     * 内嵌 iframe 将地址写进 frameSrc 属性里；外链将地址写进 name 属性里
     *
     * @param menu
     * @return
     */
    void getIframeUrl(SysMenu menu, Map<String, Object> menuMap, Map<String, Object> metaMap) {
        String isFrame = menu.getIsFrame();
        if (isFrame.equals("0")) {
            String targetType = menu.getTargetType();
            UUID uuid = UUID.fastUUID();
            if (targetType.equals("_blank")) {
                //外链将地址写进 name 属性里
                menuMap.put("name", menu.getPath());
                // path必须写，必须以 / 开头，推荐格式 / 后跟随意单词，不同的外链path不要重复哦
                menuMap.put("path", "/iframe" + uuid);
            } else {
                metaMap.put("frameSrc", menu.getPath());
                menuMap.put("path", "/iframe" + uuid);
                menuMap.put("name", "iframe_name" + uuid);
            }
        }
    }

    String getPath(String path) {
        return path.startsWith("/") ? path : "/" + path;
    }

    Map<String, Object> buildMap(SysMenu p, boolean hasChild) {
//        HashMap<String, Object> hashMap = new HashMap<String, Object>() {{
//            put("component", p.getComponent());
//            put("id", p.getMenuId());
//            put("pid", p.getParentId());
//            put("isCache", Integer.parseInt(p.getIsCache())==0);
//            put("isDisable", Integer.parseInt(p.getStatus()));
//            put("isShow", Integer.parseInt(p.getVisible())==0);
//            put("menuIcon", p.getIcon());
//            put("menuName", p.getMenuName());
//            put("menuSort", Integer.parseInt(p.getOrderNum()));
//            put("menuType", p.getMenuType());
//            put("params", p.getQuery());
//            put("paths", p.getPath());
//            put("perms", p.getPerms());
//
//        }};
        HashMap<String, Object> menuMap = new HashMap<String, Object>();
        if (p.getPath().startsWith("http") || p.getPath().startsWith("https") || p.getPath().startsWith("www")) {
            menuMap.put("path", p.getPath());
        } else {
            menuMap.put("path", p.getPath().startsWith("/") ? p.getPath() : "/" + p.getPath());
        }
        MetaVo metaVo = MetaVo.builder()
                .title(p.getMenuName())
                .icon(p.getIcon())
                .showLink(p.getVisible().equals("0"))
                .keepAlive(p.getIsCache().equals("0"))
                .rank(Integer.parseInt(p.getOrderNum()))
                .build();
        if (p.getIsFrame().equals("0")) {
            metaVo.setFrameSrc(menuMap.get("path").toString());
        }
        if (p.getParentId() != 0) {
            menuMap.put("component", p.getComponent());
            if (p.getMenuId() == 2211) {
                menuMap.put("name", "test2");
            } else if (p.getMenuId() == 2111) {
                menuMap.put("name", "PureTable");
            } else if (p.getMenuId() == 2112) {
                menuMap.put("name", "PureTableHigh");
            } else if (p.getMenuId() == 2113) {
                menuMap.put("name", "PureTableEdit");
            } else {
                menuMap.put("name", "VxeTable");
            }
        }


        if (hasChild) {
            metaVo.setShowParent(true);
        }
        menuMap.put("meta", metaVo);
        return menuMap;
    }

    @Override
    public List<Map<String, Object>> buildMenusByMap(List<SysMenu> menus) {
        List<Map<String, Object>> routers = menus.stream().filter(p -> p.getParentId() == 0)
                .map(p -> {
                    List<SysMenu> children = p.getChildren();
                    Map<String, Object> objectMap = buildMap(p, CollectionUtil.isNotEmpty(children));
                    objectMap.put("children", buildMenuChildren(p, children));
                    return objectMap;
                }).collect(Collectors.toList());

        return routers;


    }

    private List<Map<String, Object>> buildMenuChildren(SysMenu menu, List<SysMenu> menus) {
        List<Map<String, Object>> children = menus.stream().filter(p -> p.getParentId().equals(menu.getMenuId())).map(p -> {
            List<SysMenu> children1 = p.getChildren();
            Map<String, Object> objectMap = buildMap(p, CollectionUtil.isNotEmpty(children1));
            objectMap.put("children", buildMenuChildren(p, children1));
            return objectMap;
        }).collect(Collectors.toList());
        return children;

    }


    /**
     * 构建前端所需要树结构
     *
     * @param menus 菜单列表
     * @return 树结构列表
     */
    @Override
    public List<SysMenu> buildMenuTree(List<SysMenu> menus) {
        List<SysMenu> returnList = new ArrayList<SysMenu>();
        List<Long> tempList = new ArrayList<Long>();
        for (SysMenu dept : menus) {
            tempList.add(dept.getMenuId());
        }
        for (Iterator<SysMenu> iterator = menus.iterator(); iterator.hasNext(); ) {
            SysMenu menu = (SysMenu) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(menu.getParentId())) {
                recursionFn(menus, menu);
                returnList.add(menu);
            }
        }
        if (returnList.isEmpty()) {
            returnList = menus;
        }
        return returnList;
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param menus 菜单列表
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeSelect> buildMenuTreeSelect(List<SysMenu> menus) {
        List<SysMenu> menuTrees = buildMenuTree(menus);
        return menuTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 根据菜单ID查询信息
     *
     * @param menuId 菜单ID
     * @return 菜单信息
     */
    @Override
    public SysMenu selectMenuById(Long menuId) {
        return menuMapper.selectMenuById(menuId);
    }

    @Override
    public List<SysMenu> selectMenu(Set<Long> ids) {
        return menuMapper.selectMenu(ids);
    }

    /**
     * 是否存在菜单子节点
     *
     * @param menuId 菜单ID
     * @return 结果
     */
    @Override
    public boolean hasChildByMenuId(Long menuId) {
        int result = menuMapper.hasChildByMenuId(menuId);
        return result > 0 ? true : false;
    }

    /**
     * 查询菜单使用数量
     *
     * @param menuId 菜单ID
     * @return 结果
     */
    @Override
    public boolean checkMenuExistRole(Long menuId) {
        int result = roleMenuMapper.checkMenuExistRole(menuId);
        return result > 0 ? true : false;
    }

    /**
     * 新增保存菜单信息
     *
     * @param menu 菜单信息
     * @return 结果
     */
    @Override
    public int insertMenu(SysMenu menu) {
        return menuMapper.insertMenu(menu);
    }

    /**
     * 修改保存菜单信息
     *
     * @param menu 菜单信息
     * @return 结果
     */
    @Override
    public int updateMenu(SysMenu menu) {
        return menuMapper.updateMenu(menu);
    }

    /**
     * 删除菜单管理信息
     *
     * @param menuId 菜单ID
     * @return 结果
     */
    @Override
    public int deleteMenuById(Long menuId) {
        return menuMapper.deleteMenuById(menuId);
    }

    /**
     * 校验菜单名称是否唯一
     *
     * @param menu 菜单信息
     * @return 结果
     */
    @Override
    public String checkMenuNameUnique(SysMenu menu) {
        Long menuId = Objects.isNull(menu.getMenuId()) ? -1L : menu.getMenuId();
        SysMenu info = menuMapper.checkMenuNameUnique(menu.getMenuName(), menu.getParentId());
        if (Objects.nonNull(info) && info.getMenuId().longValue() != menuId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 获取路由名称
     *
     * @param menu 菜单信息
     * @return 路由名称
     */
    public String getRouteName(SysMenu menu) {
        String routerName = StringUtils.capitalize(menu.getPath());
        // 非外链并且是一级目录（类型为目录）
        if (isMenuFrame(menu)) {
            routerName = StringUtils.EMPTY;
        }
        return routerName;
    }

    /**
     * 获取路由地址
     *
     * @param menu 菜单信息
     * @return 路由地址
     */
    public String getRouterPath(SysMenu menu) {
        String routerPath = menu.getPath();
        // 内链打开外网方式
        if (menu.getParentId().intValue() != 0 && isInnerLink(menu)) {
            routerPath = innerLinkReplaceEach(routerPath);
        }
        // 非外链并且是一级目录（类型为目录）
        if (0 == menu.getParentId().intValue() && UserConstants.TYPE_DIR.equals(menu.getMenuType())
                && UserConstants.NO_FRAME.equals(menu.getIsFrame())) {
            routerPath = "/" + menu.getPath();
        }
        // 非外链并且是一级目录（类型为菜单）
        else if (isMenuFrame(menu)) {
            routerPath = "/";
        }
        return routerPath;
    }

    /**
     * 获取组件信息
     *
     * @param menu 菜单信息
     * @return 组件信息
     */
    public String getComponent(SysMenu menu) {
        String component = UserConstants.LAYOUT;
        if (StringUtils.isNotEmpty(menu.getComponent()) && !isMenuFrame(menu)) {
            component = menu.getComponent();
        } else if (StringUtils.isEmpty(menu.getComponent()) && menu.getParentId().intValue() != 0 && isInnerLink(menu)) {
            component = UserConstants.INNER_LINK;
        } else if (StringUtils.isEmpty(menu.getComponent()) && isParentView(menu)) {
            component = UserConstants.PARENT_VIEW;
        }
        return component;
    }

    /**
     * 是否为菜单内部跳转
     *
     * @param menu 菜单信息
     * @return 结果
     */
    public boolean isMenuFrame(SysMenu menu) {
        return menu.getParentId().intValue() == 0 && UserConstants.TYPE_MENU.equals(menu.getMenuType())
                && menu.getIsFrame().equals(UserConstants.NO_FRAME);
    }

    /**
     * 是否为内链组件
     *
     * @param menu 菜单信息
     * @return 结果
     */
    public boolean isInnerLink(SysMenu menu) {
        return menu.getIsFrame().equals(UserConstants.NO_FRAME) && com.chenyun.common.utils.StringUtils.isHttp(menu.getPath());
    }

    /**
     * 是否为parent_view组件
     *
     * @param menu 菜单信息
     * @return 结果
     */
    public boolean isParentView(SysMenu menu) {
        return menu.getParentId().intValue() != 0 && UserConstants.TYPE_DIR.equals(menu.getMenuType());
    }

    /**
     * 根据父节点的ID获取所有子节点
     *
     * @param list     分类表
     * @param parentId 传入的父节点ID
     * @return String
     */
    public List<SysMenu> getChildPerms(List<SysMenu> list, int parentId) {
        List<SysMenu> returnList = new ArrayList<SysMenu>();
        for (Iterator<SysMenu> iterator = list.iterator(); iterator.hasNext(); ) {
            SysMenu t = (SysMenu) iterator.next();
            // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (t.getParentId() == parentId) {
                recursionFn(list, t);
                returnList.add(t);
            }
        }
        return returnList;
    }

    /**
     * 递归列表
     *
     * @param list
     * @param t
     */
    private void recursionFn(List<SysMenu> list, SysMenu t) {
        // 得到子节点列表
        List<SysMenu> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SysMenu tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<SysMenu> getChildList(List<SysMenu> list, SysMenu t) {
        List<SysMenu> tlist = new ArrayList<SysMenu>();
        Iterator<SysMenu> it = list.iterator();
        while (it.hasNext()) {
            SysMenu n = (SysMenu) it.next();
            if (n.getParentId().longValue() == t.getMenuId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SysMenu> list, SysMenu t) {
        return getChildList(list, t).size() > 0 ? true : false;
    }

    /**
     * 内链域名特殊字符替换
     *
     * @return
     */
    public String innerLinkReplaceEach(String path) {
        return StringUtils.replaceEach(path, new String[]{SysConstants.HTTP, SysConstants.HTTPS},
                new String[]{"", ""});
    }
}
